/********************************exclude software patents********************************
Main purpose: repeat the event studies and DID analyses after excluding software patents
datasets:
us9803: patent applications filed at the USPTO from 1998 through 2003. 
us_ep_twin: US-EP twins, constructed based on data from PATSTAT 2017

********************************difference-in-difference analysis of US-EP Twins********************************/



***********************************Table I1 Panel A***********************************
//define software patents following Graham & Vishnubhakat 2013 JEP
use "$DATA\us9803",clear //definition based on USPC
gen software=0
gen artsub_num=regexr(artsubcls, "[A-Z]", "0") 
replace artsub_num=regexr(artsub_num, "[A-Z]", "0") 
destring artsub_num, replace force
replace software=1 if artcls=="029" & (artsub_num>=26000 & artsub_num<=65000)
replace software=1 if artcls=="029" & (artsub_num>=560000 & artsub_num<=566400)
replace software=1 if artcls=="029" & artsub_num==650000
replace software=1 if artcls=="073" & (artsub_num>=455000 & artsub_num<=487000)
replace software=1 if artcls=="073" & (artsub_num>=570000 & artsub_num<=669000)
replace software=1 if artcls=="084" & (artsub_num>=600000 & artsub_num<=746000)
replace software=1 if artcls=="244" & ((artsub_num>=3100 & artsub_num<=3300) | artsub_num==14000)
replace software=1 if inlist(artcls,"235","236","250","257","307","315")
replace software=1 if artcls=="318" & (artsub_num>=700000 & artsub_num<=832000)
replace software=1 if inlist(artcls,"320","323","324","326","327","330","331")
replace software=1 if artcls=="340" & (artsub_num>=850000 & artsub_num<=870440)
replace software=1 if artcls=="340" & (artsub_num>=2100 & artsub_num<=10600)
replace software=1 if artcls=="340" & (artsub_num>=825000 & artsub_num<=825980)
replace software=1 if artcls=="340" & (artsub_num>=286010 & artsub_num<=693900)
replace software=1 if artcls=="340" & (artsub_num>=901000 & artsub_num<=999000)
replace software=1 if artcls=="340" & (artsub_num>=815400 & artsub_num<=815730)
replace software=1 if artcls=="340" & (artsub_num>=815740 & artsub_num<=815920)

replace software=1 if artcls=="341" & (artsub_num>=20000 & artsub_num<=35000)
replace software=1 if artcls=="341" & (artsub_num>=173000 & artsub_num<=192000)
replace software=1 if artcls=="341" & (artsub_num>=1000 & artsub_num<=17000)
replace software=1 if artcls=="341" & (artsub_num>=50000 & artsub_num<=172000)
replace software=1 if artcls=="341" & (artsub_num>=200000 & artsub_num<=899000)

replace software=1 if artcls=="342" & (artsub_num>=1000 & artsub_num<=465000)
replace software=1 if artcls=="343" 
replace software=1 if artcls=="345" & (artsub_num>=1100 & artsub_num<=215000)
replace software=1 if artcls=="345" & (artsub_num>=418000 & artsub_num<=428000)
replace software=1 if artcls=="345" & (artsub_num>=440000 & artsub_num<=472300)
replace software=1 if artcls=="345" & (artsub_num>=473000 & artsub_num<=475000)
replace software=1 if artcls=="345" & (artsub_num>=501000 & artsub_num<=517000)
replace software=1 if artcls=="345" & (artsub_num>=518000 & artsub_num<=689000)
replace software=1 if artcls=="345" & (artsub_num>=690000 & artsub_num<=698000)
replace software=1 if artcls=="345" & artsub_num==699000
replace software=1 if inlist(artcls,"348","353","355","363","365","368")

replace software=1 if artcls=="356" & (artsub_num>=2000 & artsub_num<=3000)
replace software=1 if artcls=="356" & (artsub_num>=4090 & artsub_num<=4100)
replace software=1 if artcls=="356" & (artsub_num>=6000 & artsub_num<=27000)
replace software=1 if artcls=="356" & (artsub_num>=30000 & artsub_num<=139000)
replace software=1 if artcls=="356" & artsub_num==14000
replace software=1 if artcls=="356" & (artsub_num>=142000 & artsub_num<=151000)
replace software=1 if artcls=="356" & (artsub_num>=153000 & artsub_num<=900000)

replace software=1 if artcls=="358" & (artsub_num>=1100 & artsub_num<=3320)
replace software=1 if artcls=="358" & (artsub_num>=260000 & artsub_num<=517000)
replace software=1 if artcls=="358" & (artsub_num>=518000 & artsub_num<=540000)
replace software=1 if artcls=="359" & (artsub_num>=326000 & artsub_num<=332000)
replace software=1 if artcls=="361" & (artsub_num>=1000 & artsub_num<=270000)
replace software=1 if artcls=="361" & artsub_num==437000

replace software=1 if artcls=="367" & (artsub_num>=1000 & artsub_num<=8000)
replace software=1 if artcls=="367" & (artsub_num>=10000 & artsub_num<=13000)
replace software=1 if artcls=="367" & (artsub_num>=14000 & artsub_num<=80000)
replace software=1 if artcls=="367" & (artsub_num>=81000 & artsub_num<=85000)
replace software=1 if artcls=="367" & (artsub_num>=87000 & artsub_num<=92000)
replace software=1 if artcls=="367" & (artsub_num>=93000 & artsub_num<=94000)
replace software=1 if artcls=="367" & (artsub_num>=95000 & artsub_num<=191000)
replace software=1 if artcls=="367" & (artsub_num>=197000 & artsub_num<=199000)
replace software=1 if artcls=="367" & (artsub_num>=900000 & artsub_num<=910000)
replace software=1 if artcls=="367" & (artsub_num>=911000 & artsub_num<=912000)
replace software=1 if artcls=="367" & artsub_num==9000
replace software=1 if artcls=="367" & artsub_num==86000

replace software=1 if artcls=="369" & (artsub_num>=1000 & artsub_num<=32000)
replace software=1 if artcls=="369" & (artsub_num>=43000 & artsub_num<=54000)
replace software=1 if artcls=="369" & (artsub_num>=58000 & artsub_num<=62000)
replace software=1 if artcls=="369" & (artsub_num>=69000 & artsub_num<=70000)
replace software=1 if artcls=="369" & (artsub_num>=83000 & artsub_num<=95000)
replace software=1 if artcls=="369" & (artsub_num>=100000 & artsub_num<=126000)
replace software=1 if artcls=="369" & (artsub_num>=128000 & artsub_num<=152000)
replace software=1 if artcls=="369" & (artsub_num>=174000 & artsub_num<=175000)
replace software=1 if artcls=="369" & (artsub_num>=275100 & artsub_num<=276000)
replace software=1 if artcls=="369" & artsub_num==64000
replace software=1 if artcls=="369" & artsub_num==97000
replace software=1 if artcls=="369" & artsub_num==300000

replace software=1 if inlist(artcls,"370","374","375","380","381")
replace software=1 if inlist(artcls,"382","385","386","398","455")

replace software=1 if artcls=="378" & (artsub_num>=4000 & artsub_num<=20000)
replace software=1 if artcls=="378" & (artsub_num>=210000 & artsub_num<=901000)
replace software=1 if artcls=="379" & (artsub_num>=67100 & artsub_num<=88280)
replace software=1 if artcls=="379" & (artsub_num>=188000 & artsub_num<=337000)
replace software=1 if artcls=="396" & (artsub_num>=48000 & artsub_num<=304000)
replace software=1 if artcls=="396" & (artsub_num>=310000 & artsub_num<=321000)
replace software=1 if artcls=="396" & (artsub_num>=373000 & artsub_num<=386000)
replace software=1 if artcls=="396" & (artsub_num>=406000 & artsub_num<=410000)
replace software=1 if artcls=="396" & (artsub_num>=449000 & artsub_num<=501000)
replace software=1 if artcls=="396" & (artsub_num>=505000 & artsub_num<=510000)
replace software=1 if artcls=="396" & (artsub_num>=529000 & artsub_num<=533000)
replace software=1 if artcls=="396" & artsub_num==28000
replace software=1 if artcls=="396" & artsub_num==421000
replace software=1 if artcls=="396" & artsub_num==563000

replace software=1 if artcls=="438" & (artsub_num>=689000 & artsub_num<=698000)
replace software=1 if artcls=="438" & (artsub_num>=704000 & artsub_num<=757000)
replace software=1 if artcls=="438" & artsub_num==9000

replace software=1 if artcls=="463" & (artsub_num>=1000 & artsub_num<=47000)
replace software=1 if artcls=="463" & (artsub_num>=48000 & artsub_num<=69000)

replace software=1 if artcls=="473" & (artsub_num>=140000 & artsub_num<=141000)
replace software=1 if artcls=="473" & (artsub_num>=151000 & artsub_num<=156000)
replace software=1 if artcls=="473" & inlist(artsub_num,65000,70000,136000, 407000)

replace software=1 if artcls=="482" & (artsub_num>=1000 & artsub_num<=9000)
replace software=1 if artcls=="482" & (artsub_num>=51000 & artsub_num<=53000)
replace software=1 if artcls=="482" & (artsub_num>=57000 & artsub_num<=65000)
replace software=1 if artcls=="482" & (artsub_num>=69000 & artsub_num<=70000)
replace software=1 if artcls=="482" & (artsub_num>=112000 & artsub_num<=113000)

replace software=1 if artcls=="600" & (artsub_num>=1000 & artsub_num<=15000)
replace software=1 if artcls=="600" & (artsub_num>=19000 & artsub_num<=41000)
replace software=1 if artcls=="600" & (artsub_num>=300000 & artsub_num<=406000)
replace software=1 if artcls=="600" & (artsub_num>=407000 & artsub_num<=480000)
replace software=1 if artcls=="600" & (artsub_num>=481000 & artsub_num<=507000)
replace software=1 if artcls=="600" & (artsub_num>=529000 & artsub_num<=595000)
replace software=1 if artcls=="600" & (artsub_num>=920000 & artsub_num<=921000)

replace software=1 if artcls=="606" & (artsub_num>=1000 & artsub_num<=52000)
replace software=1 if artcls=="606" & (artsub_num>=163000 & artsub_num<=164000)
replace software=1 if artcls=="623" & (artsub_num>=24000 & artsub_num<=26000)

replace software=1 if inlist(artcls,"700","701","702","704","705","706","707")
replace software=1 if inlist(artcls,"708","709","710","711","712","713")

replace software=1 if artcls=="703" & (artsub_num>=1000 & artsub_num<=10000)
replace software=1 if artcls=="703" & (artsub_num>=11000 & artsub_num<=12000)
replace software=1 if artcls=="703" & (artsub_num>=13000 & artsub_num<=999000)

replace software=1 if artcls=="714" & (artsub_num>=1000 & artsub_num<=100000)
replace software=1 if artcls=="714" & (artsub_num>=699000 & artsub_num<=824000)

replace software=1 if inlist(artcls,"715","716","717","718","719","725","726","901","902")




log using "$DATA\tableI1_panelA_log.txt", replace 
preserve 
****exclude software patents*****
drop if software==1

qui: reghdfe lnfcite3 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Fcite3Y) nocons bdec(3)  replace
lincom_prepost 37

qui: reghdfe lnfcite5 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Fcite5Y) nocons bdec(3) 
lincom_prepost 37

qui: reghdfe lnfcite7 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Fcite7Y) nocons bdec(3) 
lincom_prepost 37

qui: reghdfe lnfcite10 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Fcite10Y) nocons bdec(3) 
lincom_prepost 37


qui: reghdfe lnlag1 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Fcite3Y) nocons bdec(3)  
lincom_prepost 37

qui: reghdfe lnlag3 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Fcite5Y) nocons bdec(3) 
lincom_prepost 37

qui: reghdfe lnlag5 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Fcite7Y) nocons bdec(3) 
lincom_prepost 37

qui: reghdfe lnlag7 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Fcite10Y) nocons bdec(3) 
lincom_prepost 37



qui: reghdfe ipc7_50 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Sim50) nocons bdec(3)  
lincom_prepost 37

qui: reghdfe ipc7_75 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Sim75) nocons bdec(3) 
lincom_prepost 37

qui: reghdfe ipc7_90 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Sim90) nocons bdec(3) 
lincom_prepost 37

qui: reghdfe ipc7_95 month i.monthdum grant18m doptout, absorb(artcls ) vce(cluster month)
outreg2 using "$DATA\tableI1_panelA.xls", ctitle(Sim95) nocons bdec(3) 
lincom_prepost 37
restore 
rm "$DATA\tableI1_panelA.txt"
log close





***********************************Table I1 Panel B***********************************
*get appln_id 
keep appl_id software 
joinby appl_id using "$DATA\appid_uspto_epstat", unmatched(master) //linking US patent applications from USPTO to PATSTAT
tab _merge 
/*
          only in master data |    221,223       14.40       14.40
both in master and using data |  1,315,123       85.60      100.00
*/
keep if _merge==3
drop _merge 
keep appln_id software 
tempfile software 
save `software', replace 




use "$DATA\us_ep_twin",clear
joinby appln_id using `software', unmatched(master)
tab _merge if uspto==1 
tab software if uspto==1 
drop _merge
sort appln_id_us software 
by appln_id_us: replace software=software[_n-1] if software==. //extropolate to EP twins

preserve 
drop if software==1
*citation
qui: reghdfe lnfcite_pub3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableI1_panelB.xls", ctitle(fcite_pub3Y)  nocons bdec(3)  keep(`r(X)')  replace
qui: reghdfe lnfcite_pub5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableI1_panelB.xls", ctitle(fcite_pub5Y)  nocons bdec(3)  keep(`r(X)') 
qui: reghdfe lnfcite_pub7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableI1_panelB.xls", ctitle(fcite_pub7Y)  nocons bdec(3)  keep(`r(X)') 
qui: reghdfe lnfcite_pub10 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableI1_panelB.xls", ctitle(fcite_pub10Y)  nocons bdec(3)  keep(`r(X)') 


*citation lag 
qui: reghdfe lnlag1 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableI1_panelB.xls", ctitle(Lag1)  nocons bdec(3)  keep(`r(X)')  
qui: reghdfe lnlag3 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableI1_panelB.xls", ctitle(Lag3)  nocons bdec(3)  keep(`r(X)') 
qui: reghdfe lnlag5 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableI1_panelB.xls", ctitle(Lag5)  nocons bdec(3)  keep(`r(X)') 
qui: reghdfe lnlag7 i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableI1_panelB.xls", ctitle(Lag7)  nocons bdec(3)  keep(`r(X)') 

*similarity 
foreach i of numlist 50 75 90 95{
qui: reghdfe ipc7_`i' i.uspto##i.post_aipa granted i.uspto##i.grant18m, absorb(appln_id_us month) vce(cluster month_country)
indeplist
outreg2 using "$DATA\tableI1_panelB.xls", ctitle(Sim`i') keep(`r(X)') nocons dec(3) 
}
rm "$DATA\tableI1_panelB.txt"
restore 

